package com.limao.DBProvider;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.limao.DBDataClass.CrowdPrestigeGoodsDataClass;
import com.limao.DBDataClass.RedbagPrize;
import com.limao.DBDataClass.SysRedbagClass;
import com.limao.DbConnection.DbConnectionManager;
import com.limao.Tool.CacheClass;

public class SysRedbagProvider {

	private static class DB_Adv_ProviderContainer {
		private static SysRedbagProvider instance = new SysRedbagProvider();
	}

	public static SysRedbagProvider getInstance() {
		return DB_Adv_ProviderContainer.instance;
	}
	
	// 初始化
	private SysRedbagProvider() {

	}
	
	private SysRedbagClass encapOrderData(ResultSet rs) throws Exception{
		
		SysRedbagClass orderData = new SysRedbagClass();
	
		orderData.redbagid= rs.getString("redbagid");
		orderData.redbagname= rs.getString("redbagname");
		orderData.redbagimage= rs.getString("redbagimage");
		
		RedbagPrize p0=new RedbagPrize();
		p0.sellOne="0";
		p0.rmbtype = rs.getString("rmbtype0");
		p0.rmb = rs.getString("rmb0");
		p0.prestigeid = rs.getString("prestigeid0");
		p0.rate = rs.getString("rate0");
		orderData.prizelist.add(p0);

		RedbagPrize p1=new RedbagPrize();
		p1.sellOne="1";
		p1.rmbtype = rs.getString("rmbtype1");
		p1.rmb = rs.getString("rmb1");
		p1.prestigeid = rs.getString("prestigeid1");
		p1.rate = rs.getString("rate1");
		orderData.prizelist.add(p1);	
		
		RedbagPrize p2=new RedbagPrize();
		p2.sellOne="2";
		p2.rmbtype = rs.getString("rmbtype2");
		p2.rmb = rs.getString("rmb2");
		p2.prestigeid = rs.getString("prestigeid2");
		p2.rate = rs.getString("rate2");
		orderData.prizelist.add(p2);
		
		RedbagPrize p3=new RedbagPrize();
		p3.sellOne="3";
		p3.rmbtype = rs.getString("rmbtype3");
		p3.rmb = rs.getString("rmb3");
		p3.prestigeid = rs.getString("prestigeid3");
		p3.rate = rs.getString("rate3");
		orderData.prizelist.add(p3);
		
		RedbagPrize p4=new RedbagPrize();
		p4.sellOne="4";
		p4.rmbtype = rs.getString("rmbtype4");
		p4.rmb = rs.getString("rmb4");
		p4.prestigeid = rs.getString("prestigeid4");
		p4.rate = rs.getString("rate4");
		orderData.prizelist.add(p4);
		
		RedbagPrize p5=new RedbagPrize();
		p5.sellOne="5";
		p5.rmbtype = rs.getString("rmbtype5");
		p5.rmb = rs.getString("rmb5");
		p5.prestigeid = rs.getString("prestigeid5");
		p5.rate = rs.getString("rate5");
		orderData.prizelist.add(p5);
		
		RedbagPrize p6=new RedbagPrize();
		p6.sellOne="6";
		p6.rmbtype = rs.getString("rmbtype6");
		p6.rmb = rs.getString("rmb6");
		p6.prestigeid = rs.getString("prestigeid6");
		p6.rate = rs.getString("rate6");
		orderData.prizelist.add(p6);
		
		RedbagPrize p7=new RedbagPrize();
		p7.sellOne="7";
		p7.rmbtype = rs.getString("rmbtype7");
		p7.rmb = rs.getString("rmb7");
		p7.prestigeid = rs.getString("prestigeid7");
		p7.rate = rs.getString("rate7");
		orderData.prizelist.add(p7);
		
		RedbagPrize p8=new RedbagPrize();
		p8.sellOne="8";
		p8.rmbtype = rs.getString("rmbtype8");
		p8.rmb = rs.getString("rmb8");
		p8.prestigeid = rs.getString("prestigeid8");
		p8.rate = rs.getString("rate8");
		orderData.prizelist.add(p8);
		
		RedbagPrize p9=new RedbagPrize();
		p9.sellOne="9";
		p9.rmbtype = rs.getString("rmbtype9");
		p9.rmb = rs.getString("rmb9");
		p9.prestigeid = rs.getString("prestigeid9");
		p9.rate = rs.getString("rate9");
		orderData.prizelist.add(p9);
		
		
		RedbagPrize p10=new RedbagPrize();
		p10.sellOne="10";
		p10.rmbtype = rs.getString("rmbtype10");
		p10.rmb = rs.getString("rmb10");
		p10.prestigeid = rs.getString("prestigeid10");
		p10.rate = rs.getString("rate10");
		orderData.prizelist.add(p10);
		
		
		RedbagPrize p11=new RedbagPrize();
		p11.sellOne="11";
		p11.rmbtype = rs.getString("rmbtype11");
		p11.rmb = rs.getString("rmb11");
		p11.prestigeid = rs.getString("prestigeid11");
		p11.rate = rs.getString("rate11");
		orderData.prizelist.add(p11);
		
		RedbagPrize p12=new RedbagPrize();
		p12.sellOne="12";
		p12.rmbtype = rs.getString("rmbtype12");
		p12.rmb = rs.getString("rmb12");
		p12.prestigeid = rs.getString("prestigeid12");
		p12.rate = rs.getString("rate12");
		orderData.prizelist.add(p12);
		
		RedbagPrize p13=new RedbagPrize();
		p13.sellOne="13";
		p13.rmbtype = rs.getString("rmbtype13");
		p13.rmb = rs.getString("rmb13");
		p13.prestigeid = rs.getString("prestigeid13");
		p13.rate = rs.getString("rate13");
		orderData.prizelist.add(p13);
		
		
		RedbagPrize p14=new RedbagPrize();
		p14.sellOne="14";
		p14.rmbtype = rs.getString("rmbtype14");
		p14.rmb = rs.getString("rmb14");
		p14.prestigeid = rs.getString("prestigeid14");
		p14.rate = rs.getString("rate14");
		orderData.prizelist.add(p14);
		
		RedbagPrize p15=new RedbagPrize();
		p15.sellOne="15";
		p15.rmbtype = rs.getString("rmbtype15");
		p15.rmb = rs.getString("rmb15");
		p15.prestigeid = rs.getString("prestigeid15");
		p15.rate = rs.getString("rate15");
		orderData.prizelist.add(p15);
		
		for(RedbagPrize p:orderData.prizelist)  
		{
			if(CacheClass.getIntValue(p.rmbtype)==1 && CacheClass.getIntValue(p.prestigeid)>0) //0金额 1实物  2现货瓶数
			{
				CrowdPrestigeGoodsDataClass obj = CrowdPrestigeGoodsProvider.getInstance().GetRedBagGoods(CacheClass.getIntValue(p.prestigeid));
				if(obj!=null)
				{
					p.pgoodsname= obj.prestigename;
					p.pgoodsimage= obj.prestigeimage;
				}
				
			}
		}
		
	
		return orderData;
		
	}
	
	public SysRedbagClass Load_SysRedbagByid(int redbagid) {
		
		SysRedbagClass obj = null ;
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			con = DbConnectionManager.getConnection();
			pstmt = con.prepareStatement("select * from cd_sys_redbag where redbagid=?");
			pstmt.setInt(1, redbagid);
			rs = pstmt.executeQuery();
			if (rs.next()) {
				obj = encapOrderData(rs);
			}
		} catch (Exception e) {
			obj=null;
			e.printStackTrace();
		} finally {
			DbConnectionManager.closeConnection(rs, pstmt, con);
		}
		return obj;

	}
	
	
}
